import { Controller, Get, Post, Put, Delete, Body, Param } from 'routing-controllers'
import { UserService } from '../service/UserService';
import { Inject, Service } from 'typedi'
import { User } from '../entity/User';

@Controller('/user')
export class UserController {

    userService
    constructor() {
        this.userService = new UserService();
    }
 
    @Get('/queryUserList')
    async queryUserList() {
        try {
            const result = await this.userService.queryUserList()
            return result.data; 
        } catch (error) {
            console.error('Error fetching user list:', error)
            throw new Error('Failed to fetch user list')
        }
    }

    @Post('/createUser')
    async createUser(@Body() createUserDto: User): Promise<User> { 
        const user = this.userService.save(createUserDto); 
        return user; 
    }
    
    @Post('/updateUser') 
    async updateUser(@Body() user: User): Promise<User> { 
        return this.userService.save(user);
    }
    
    // 定义一个DELETE路由，用于删除用户。
    @Post('/deleteUser/:id') 
    async deleteUser(@Param('id') id: number): Promise<void> { 
        await this.userService.delete(id); 
    }

}

